package com.zhongwang.business.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 *  NumberUtils
 *  数字工具
 * @author 徐一贺
 * @date 2020/10/12 19:54
 * @version 1.0
      */
public class NumberUtils {
    /**  intToBig 小写转大写
     * @param src
     * @return java.lang.String
     * @author 徐一贺
     * @date 2021/1/7 12:45
     */
   public static String intToBig(int src) {
        final String num[] = {"〇", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        final String unit[] = {"元", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千"};
        String dst = "";
        int count = 0;
        while (src > 0) {
            dst = (num[src % 10] + unit[count]) + dst;
            src = src / 10;
            count++;
        }
        return dst.replaceAll("〇[千百十]", "〇").replaceAll("〇+万", "万")
                .replaceAll("〇+亿", "亿").replaceAll("亿万", "亿〇")
                .replaceAll("〇+", "〇").replaceAll("〇元", "").replaceAll("元", "");
    }

    private static char[] cnNum = new char[]{'〇', '一', '二', '三', '四', '五', '六', '七', '八', '九'};
    private static char[] unitArr = new char[]{'厘', '分', '角', '圆', '十', '百', '千', '万', '亿'};

    /**
     * 中文金额转数字
     * @param chineseNumber 中文金额
     * @return
     */
    public static Integer chinese2Number(String chineseNumber) {
        chineseNumber = chineseNumber+"元";
        BigDecimal result = new BigDecimal(0);
        int lastUnitIndex = 0, num = 0;
        chineseNumber = chineseNumber.replace("元", "圆");
        for (int i = 0; i < chineseNumber.length(); i++) {
            boolean isUnit = true;
            char c = chineseNumber.charAt(i);
            for (int j = 0; j < cnNum.length; j++) {
                // 是数字
                if (c == cnNum[j]) {
                    // 数字值 = 索引
                    num = j;
                    isUnit = false;
                    break;
                }
            }
            if (isUnit) {
                // 第一个就是单位，如：拾伍万圆整
                if (i == 0) {
                    num = 1;
                }
                int unitIndex = getUnitIndex(c);
                BigDecimal unit = getUnit(c);
                if (unitIndex > lastUnitIndex) {
                    result = result.add(new BigDecimal(num)).multiply(unit);
                } else {
                    result = result.add(new BigDecimal(num).multiply(unit));
                }
                lastUnitIndex = unitIndex;
                num = 0;
            }
        }
        return result.setScale(0,BigDecimal.ROUND_DOWN).intValue();
    }

    private static int getUnitIndex(char c) {
        for (int j = 0; j < unitArr.length; j++) {
            if (c == unitArr[j]) {
                return j;
            }
        }
        return 0;
    }

    private static BigDecimal getUnit(char c) {
        double num = 0;
        int unitIndex = getUnitIndex(c);
        switch (unitIndex) {
            // '厘', '分', '角', '圆', '拾', '佰', '仟', '万', '亿'
            case 4:
                num = 10;
                break;
            case 5:
                num = 100;
                break;
            case 6:
                num = 1000;
                break;
            case 7:
                num = 10000;
                break;
            case 8:
                num = 100000000;
                break;
            case 3:
                num = 1;
                break;
            case 2:
                num = 0.1;
                break;
            case 1:
                num = 0.01;
                break;
            case 0:
                num = 0.001;
                break;
            default:
                break;
        }
        return new BigDecimal(num);
    }

}
